<template>
	<view class="points-page topBg">
		<!-- 顶部导航 -->
		<view class="nav-bar">
			<img src="/static/back.png" alt="" class="back-icon" @click="backmine" />
			<view class="title" v-if="state.userName">{{state.userName}}的积分({{state.allPoint}})</view>
			<view class="title" v-else>我的积分({{state.allPoint}})</view>
		</view>


		<!-- 积分列表modal -->
		<view class="points-modal">

			<view class="points-list" v-if="state.pointsList.length > 0">
				<view class="points-item" v-for="(item, index) in state.pointsList" :key="index">
					<view class="left">
						<view class="icon">
							<image :src="item.icon" mode="aspectFit"></image>
						</view>
						<view class="info">
							<view class="name">{{ item.change_detail }}</view>
							<view class="date">{{ item.person_name }}</view>
						</view>
					</view>
					<view class="points" :class="{ 'points-add': item.jackpoint > 0 }">
						<!-- {{ item.jackpoint > 0 ? "+" : "" }} -->
						{{ Number(item.jackpoint) }}积分
					</view>
				</view>
			</view>

			<view class="empty-state" v-else>
				<image src="../../static/walletempty.png" mode="aspectFit" class="empty-icon"></image>
				<text class="empty-text">暂无积分记录</text>
			</view>



		</view>

		<view class="addWallt" v-if="state.usertype" @click="toAddScore">
			添加积分
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		onMounted
	} from "vue";
	import {
		onShow
	} from '@dcloudio/uni-app';
	import {
		request,
		getOptionLabel
	} from "../../js/util.js";

	const state = reactive({
		pointsList: [],
		usertype: false,
		addModal: false,
		userName: null,
		allPoint: 0
	});
	const getPointList = async () => {
		let searchId = state.usertype ? uni.getStorageSync("searchUser") : uni.getStorageSync("userid")
		let submitData = {
			condition: {
				user_id: searchId,
			}
		};
		const data = await request("selectJackpoint", submitData);
		console.log("selectJackpoint", data)
		if (data.result) {
			state.pointsList = data.result;
			let allPoint = 0
			state.pointsList.map(x => {
				allPoint += Number(x.jackpoint)
			})
			state.allPoint = allPoint
		} else {
			uni.showToast({
				title: "查询失败",
				icon: "error",
			});
		}
	};
	const backmine = () => {
		uni.navigateBack()
	}
	const toAddScore = () => {
		uni.navigateTo({
			url: '/pages/addScore/addScore'
		})
	}
	// 页面加载时获取数据
	onShow(() => {
		const userInfo = uni.getStorageSync('userInfo');
		console.log('userInfo', userInfo)
		if (userInfo.role == 'admin') {
			state.usertype = true
			state.userName = uni.getStorageSync("userName")
			console.log("state.userName", state.userName, state.usertype)
		}
		getPointList()

	});
</script>

<style lang="scss" scoped>
	.topBg {
		background-size: 100% 20%;
		background-repeat: no-repeat;
		background-image: url("");
	}

	.points-page {
		min-height: 100vh;

		.nav-bar {
			padding-top: 105rpx;
			padding-left: 20rpx;
			display: flex;
			align-items: center;

			.back-icon {
				width: 30rpx;
				height: 30rpx;
				color: #333;
				margin: 0 20rpx;
			}

			.title {
				flex: 1;
				font-size: 20px;
				font-weight: 700;
				color: #333;
				margin-right: 20px;
			}
		}

		.points-modal {
			margin: 30rpx;
			background: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.points-list {
				.points-item {
					display: flex;
					justify-content: space-between;
					// align-items: center;
					padding: 30rpx 0;
					border-bottom: 1px solid rgba(0, 0, 0, 0.05);

					&:last-child {
						border-bottom: none;
					}

					.left {
						display: flex;
						// align-items: center;

						.icon {
							width: 50rpx;
							height: 50rpx;
							margin-right: 20rpx;

							image {
								width: 100%;
								height: 100%;
							}
						}

						.info {
							.name {
								font-size: 32rpx;
								color: #333;
								margin-bottom: 10rpx;
							}

							.date {
								font-size: 24rpx;
								color: #999;
							}
						}
					}

					.points {
						font-size: 28rpx;
						color: #ff0000;

						&.points-add {
							color: #ff6b35;
						}
					}
				}
			}

			.empty-state {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 60rpx 0;

				.empty-icon {
					width: 200rpx;
					height: 200rpx;
					margin-bottom: 20rpx;
				}

				.empty-text {
					font-size: 28rpx;
					color: #999;
				}
			}
		}
	}

	.addWallt {
		width: 80%;
		margin: 20rpx auto;
		background-color: greenyellow;
		border-radius: 40rpx;
		text-align: center;
		padding: 20rpx;
	}
</style>